package com.yxx.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yxx.business.entity.*;
import com.yxx.business.enums.PhaseEnum;
import com.yxx.business.service.*;
import com.yxx.common.utils.DateUtils;
import com.yxx.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.core.service.impl.ServiceImplPlus;
import com.yxx.business.mapper.TbEquipmentConfigMapper;

/**
 * 设备配置Service业务层处理
 *
 * @author yxx
 * @date 2025-08-05
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbEquipmentConfigServiceImpl extends ServiceImplPlus<TbEquipmentConfigMapper, TbEquipmentConfigDo> implements ITbEquipmentConfigService, PhaseService {

    private final TbEquipmentConfigMapper tbEquipmentConfigMapper;

    /**
     * 查询设备配置分页结果
     *
     * @param tbEquipmentConfigQueryDto 设备配置查询实体
     * @return 设备配置分页
     */
    @Override
    public PageResult<TbEquipmentConfigVo> selectTbEquipmentConfigVoPage(TbEquipmentConfigQueryDto tbEquipmentConfigQueryDto) {
        startPage();
        PageResult<TbEquipmentConfigVo> page
                = super.getMyBatisPageResult(selectTbEquipmentConfigDoList(tbEquipmentConfigQueryDto), TbEquipmentConfigVo.class);
        clearPage();
        return page;
    }

    /**
     * 查询设备配置Vo列表
     *
     * @param tbEquipmentConfigQueryDto 设备配置查询实体
     * @return 设备配置集合
     */
    @Override
    public List<TbEquipmentConfigVo> selectTbEquipmentConfigVoList(TbEquipmentConfigQueryDto tbEquipmentConfigQueryDto) {
        return super.convertList(tbEquipmentConfigMapper.selectTbEquipmentConfigList(tbEquipmentConfigQueryDto), TbEquipmentConfigVo.class);
    }

    /**
     * 查询设备配置Do列表
     *
     * @param tbEquipmentConfigQueryDto 设备配置查询实体
     * @return 设备配置集合
     */
    @Override
    public List<TbEquipmentConfigDo> selectTbEquipmentConfigDoList(TbEquipmentConfigQueryDto tbEquipmentConfigQueryDto) {
        return tbEquipmentConfigMapper.selectTbEquipmentConfigList(tbEquipmentConfigQueryDto);
    }

    /**
     * 查询单个设备配置
     *
     * @param equipmentId 设备配置主键
     * @return 设备配置单个
     */
    @Override
    public TbEquipmentConfigVo selectTbEquipmentConfigVoByEquipmentId(Long equipmentId) {
        return super.convertBean(tbEquipmentConfigMapper.selectTbEquipmentConfigByEquipmentId(equipmentId), TbEquipmentConfigVo.class);
    }

    /**
     * 新增设备配置
     *
     * @param tbEquipmentConfigQueryDto 设备配置编辑实体
     * @return 结果
     */
    @Override
    public int insertTbEquipmentConfig(TbEquipmentConfigEditDto tbEquipmentConfigQueryDto) {
        tbEquipmentConfigQueryDto.setCreateBy(getUserNameOrNotLogged());
        tbEquipmentConfigQueryDto.setCreateTime(DateUtils.getNowDate());
        return tbEquipmentConfigMapper.insertTbEquipmentConfig(super.convertT(tbEquipmentConfigQueryDto));
    }

    /**
     * 修改设备配置
     *
     * @param equipmentId 设备配置主键
     * @param tbEquipmentConfigQueryDto 设备配置编辑实体
     * @return 结果
     */
    @Override
    public int updateTbEquipmentConfig(Long equipmentId, TbEquipmentConfigEditDto tbEquipmentConfigQueryDto) {
        tbEquipmentConfigQueryDto.setUpdateBy(getUserNameOrNotLogged());
        tbEquipmentConfigQueryDto.setUpdateTime(DateUtils.getNowDate());
        return tbEquipmentConfigMapper.updateTbEquipmentConfig(super.convertT(tbEquipmentConfigQueryDto).setEquipmentId(equipmentId));
    }

    /**
     * 批量删除设备配置
     *
     * @param equipmentIds 需要删除的设备配置主键集合
     * @return 结果
     */
    @Override
    public int deleteTbEquipmentConfigByEquipmentIds(List<Long> equipmentIds) {
        return tbEquipmentConfigMapper.deleteTbEquipmentConfigByEquipmentIds(equipmentIds);
    }

    /**
     * 删除单个设备配置信息
     *
     * @param equipmentId 设备配置主键
     * @return 结果
     */
    @Override
    public int deleteTbEquipmentConfigByEquipmentId(Long equipmentId) {
        return tbEquipmentConfigMapper.deleteTbEquipmentConfigByEquipmentId(equipmentId);
    }

    private final ITbSurveyDesignPhaseService surveyDesignPhaseService;

    private final ITbMaterialProductionPhaseService materialProductionPhaseService;

    private final ITbConstructionPhaseService constructionPhaseService;

    private final ITbOperationPhaseService operationPhaseService;

    private final ITbDemolitionPhaseService demolitionPhaseService;

    @Override
    public TbEquipmentPhaseVo getPhase(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        PhaseEnum phaseEnum = PhaseEnum.getPhaseEnum(tbEquipmentPhaseDto.getUsagePhase());
        if (PhaseEnum.survey_design_phase.equals(phaseEnum)) {
            return surveyDesignPhaseService.getPhase(tbEquipmentPhaseDto).setUsagePhase(tbEquipmentPhaseDto.getUsagePhase());
        } else if (PhaseEnum.material_production_phase.equals(phaseEnum)) {
            return materialProductionPhaseService.getPhase(tbEquipmentPhaseDto).setUsagePhase(tbEquipmentPhaseDto.getUsagePhase());
        } else if (PhaseEnum.construction_phase.equals(phaseEnum)) {
            return constructionPhaseService.getPhase(tbEquipmentPhaseDto).setUsagePhase(tbEquipmentPhaseDto.getUsagePhase());
        } else if (PhaseEnum.operation_phase.equals(phaseEnum)) {
            return operationPhaseService.getPhase(tbEquipmentPhaseDto).setUsagePhase(tbEquipmentPhaseDto.getUsagePhase());
        } else if (PhaseEnum.demolition_phase.equals(phaseEnum)) {
            return demolitionPhaseService.getPhase(tbEquipmentPhaseDto).setUsagePhase(tbEquipmentPhaseDto.getUsagePhase());
        }
        return null;
    }

    @Override
    public int savePhase(TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        PhaseEnum phaseEnum = PhaseEnum.getPhaseEnum(tbEquipmentPhaseVo.getUsagePhase());
        if (PhaseEnum.survey_design_phase.equals(phaseEnum)) {
            return surveyDesignPhaseService.savePhase(tbEquipmentPhaseVo);
        } else if (PhaseEnum.material_production_phase.equals(phaseEnum)) {
            return materialProductionPhaseService.savePhase(tbEquipmentPhaseVo);
        } else if (PhaseEnum.construction_phase.equals(phaseEnum)) {
            return constructionPhaseService.savePhase(tbEquipmentPhaseVo);
        } else if (PhaseEnum.operation_phase.equals(phaseEnum)) {
            return operationPhaseService.savePhase(tbEquipmentPhaseVo);
        } else if (PhaseEnum.demolition_phase.equals(phaseEnum)) {
            return demolitionPhaseService.savePhase(tbEquipmentPhaseVo);
        }
        return 0;
    }

    @Override
    public List<TbEquipmentPhaseVo> getPhaseAll(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        List<TbEquipmentPhaseVo> result = new ArrayList<>();
        PhaseEnum.list.forEach((item) -> {
            tbEquipmentPhaseDto.setUsagePhase(item.getPhase());
            result.add(getPhase(tbEquipmentPhaseDto));
        });
        return result;
    }

    @Override
    public int updatePhase(Long id, TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        PhaseEnum phaseEnum = PhaseEnum.getPhaseEnum(tbEquipmentPhaseVo.getUsagePhase());
        if (PhaseEnum.survey_design_phase.equals(phaseEnum)) {
            return surveyDesignPhaseService.updatePhase(id, tbEquipmentPhaseVo);
        } else if (PhaseEnum.material_production_phase.equals(phaseEnum)) {
            return materialProductionPhaseService.updatePhase(id, tbEquipmentPhaseVo);
        } else if (PhaseEnum.construction_phase.equals(phaseEnum)) {
            return constructionPhaseService.updatePhase(id, tbEquipmentPhaseVo);
        } else if (PhaseEnum.operation_phase.equals(phaseEnum)) {
            return operationPhaseService.updatePhase(id, tbEquipmentPhaseVo);
        } else if (PhaseEnum.demolition_phase.equals(phaseEnum)) {
            return demolitionPhaseService.updatePhase(id, tbEquipmentPhaseVo);
        }
        return 0;
    }
}
